<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexIntegralRecordRequest;
use App\Http\Requests\StoreIntegralRecordRequest;
use App\Http\Requests\UpdateIntegralRecordRequest;
use App\Http\Resources\IntegralRecordResource;
use App\Models\IntegralRecord;

class IntegralRecordController extends Controller
{
    public function __construct(){
        $this->middleware('auth:sanctum');
    }
    /**
     * Display a listing of the resource.
     */
    public function index(IndexIntegralRecordRequest $request)
    {
        $query = IntegralRecord::query();
        $query->when($request->filled('username'), function ($query) use ($request) {
            $username = $request->input('username');
            return $query->whereHas('users', function ($subquery) use ($username) {
                $subquery->where('username', 'like', "%$username%");
            });
        })->when($request->filled('income_type'), function ($query) use ($request) {
            $income_type = $request->input('income_type');
            return $query->where('income_type', $income_type);
        })->when($request->filled('integral_type'), function ($query) use ($request) {
            $integral_type = $request->input('integral_type');
            return $query->where('integral_type', $integral_type);
        });

        $data = $query->with(['intergralabel', 'users'])->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response =  IntegralRecordResource::collection($data);
        $total = $response->total();
        return $this->success('查询成功', $response, $total);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreIntegralRecordRequest $request)
    {
        
    }

    /**
     * Display the specified resource.
     */
    public function show(IntegralRecord $integralRecord)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(IntegralRecord $integralRecord)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateIntegralRecordRequest $request, IntegralRecord $integralRecord)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(IntegralRecord $integralRecord)
    {
        //
    }
}
